Page({

  /**
   * 页面的初始数据
   */
  data: {
    ctx:null,
    width:0,
    height:0,
    canvasHeight:0,
    seatLen:0,
    img:['/icons/person_u.png','/icons/person_d.png','/icons/person_l.png','/icons/person_r.png','/icons/fault.png'],
    widthList01:[0.494,0.494,0.535,0.56833,0.61,0.646,0.646,0.709,0.7516,0.786,0.826,0.8644,
      0.450,0.436,0.463,0.4516,0.436,0.4516,0.4516,0.436,0.463,0.4516,0.666,0.666,0.802,0.802], // 1-12,13-26
    heightList01:[0.054,0.067,0.058,0.057,0.057,0.052,0.064,0.057,0.057,0.057,0.057,0.057,
      0.107,0.1645,0.1645,0.220,0.276,0.328,0.360,0.390,0.390,0.425,0.255,0.340,0.314,0.390], // 1-12,13-26
    // 27-59。需复用
    widthList03:[0.43,0.465,0.531,0.5654,0.608,0.6426,0.685,0.72155,0.8835],
    heightList03:[0.808,0.827,0.8566,0.873,0.8362],
    intervalId:0,
    seatList:[],
    // preSeatList:[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    //   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    //   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    //   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
  },

  creatCanvas(){
      const res = wx.getSystemInfoSync();
      let w = res.windowWidth;
      let h = res.windowHeight;
      let tmpCtx = wx.createCanvasContext('canvas-id');
      // let ch = 750 / w * h;
      this.setData({
        ctx:tmpCtx,
        width: w,
        height: h,
        canvasHeight: 750 / w * h,
        seatLen: w * 0.0233333
      })
  },

  drawCanvas(){
    // 贴人儿
    this.data.ctx.draw();
    let imgNum = 3;
    let ww,hh;
    let seatLen = this.data.seatLen;
    // 位置区域 01 02
    for (let i = 0; i < 26; i++) { // 1-12,13-26 椅子
      this.data.ctx.beginPath();
      if(this.data.seatList[i]==0)continue;
      else if(this.data.seatList[i]==2)imgNum = 4;
      else if((i==2||i==4||i==8||i==10)) imgNum = 2;
      else if(i==18||i>=21) imgNum = 0;
      else if(i>=12) imgNum = 1;
      else imgNum =3;
      ww = this.data.width * this.data.widthList01[i];
      hh = this.data.height * this.data.heightList01[i];
      this.data.ctx.drawImage(this.data.img[imgNum], ww, hh, seatLen,seatLen);
      this.data.ctx.draw(true);
    }
    // 位置区域 03
    let num=25;
    for (let i = 0; i < 9; i++) { // 9列
      ww = this.data.width * this.data.widthList03[i];
      for(let j = 0; j<4; j++){ // 4行
        num++;
        if(this.data.seatList[num]==2)imgNum=4;
        else if(this.data.seatList[num]==0)continue;
        else{
          if(i%2==1) imgNum = 2;
          else imgNum = 3;
        }
        this.data.ctx.beginPath()
        if(i==8) j = 4; // 最后一列特殊
        hh = this.data.height * this.data.heightList03[j];
        this.data.ctx.drawImage(this.data.img[imgNum], ww, hh, seatLen,seatLen);
        this.data.ctx.draw(true);
      }
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.creatCanvas()
  },

  // 从后台 获取图书馆座位情况
  getSeatStatus(){
    wx.request({
      url: 'http://47.103.195.19/api/wechat/seat/state',
      data: {},
      header: {'content-type':'application/json'},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (result)=>{
        console.log("wx.request",this.data.seatList)
        var listH = result.data.objectResult.obj;
        var listY = this.data.seatList;
        console.log("ce................请求返回的数据listH",listH)
        // console.log("ce.................请求数据前，listY",listY)
        
        for(let i=0;i<59;i++){
          if(!(listY[i]==listH[i]))console.log("diffrent! index=",i,listY[i],"........",listH[i]);
        }
        // 更新新旧座位表
        // if(this.data.preSeatList[0]!=3){ // 不是第一次获取数据
          // var tmp = seatList;
          this.setData({
            seatList:result.data.objectResult.obj,
            // preSeatList:tmp
          })
          // console.log("preSeatList",preSeatList)
          // console.log("seatList",seatList)
        // }
        
        this.drawCanvas()
      },
      fail: ()=>{
        console.log("网络错误......");
      },
      complete: ()=>{}
    });

    
  },
  
  
  onReady: function () {
    this.getSeatStatus();
  },

  onShow: function () {
    console.log("onshow")
    // console.log("jin 0ne",this.data.seatList)
    let tmpIntervalId = setInterval(this.getSeatStatus,3000)
    this.setData({
      intervalId:tmpIntervalId
    })
    
  },

  onHide: function () {
    console.log("seat 页面已隐藏，销毁定时器……")
    clearInterval(this.data.intervalId)
  },

  onUnload: function () {
    console.log("seat 页面已被销毁，定时器已销毁……")
    clearInterval(this.data.intervalId)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})